<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="博客标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入博客标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">

        <el-select
          v-model="queryParams.categoryId"
          placeholder="博客分类"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="category in categoryOptions"
            :key="category.categoryId"
            :label="category.categoryName"
            :value="category.categoryId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="标签" prop="tagId">

        <el-select
          v-model="queryParams.tagId"
          placeholder="博客标签"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="tag in tagOptions"
            :key="tag.tagId"
            :label="tag.tagName"
            :value="tag.tagId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否发布" prop="isPublish">


        <el-select
          v-model="queryParams.isPublish"
          placeholder="是否发布"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="dict in dict.type.blog_publish_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['blog:blog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['blog:blog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['blog:blog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:blog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blogList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="博客ID" align="center" prop="blogId" v-if="false"/>
      <el-table-column label="发布者" align="center" prop="author" />
      <el-table-column label="博客标题" align="center" prop="title" />
      <el-table-column label="博客分类" align="center" prop="categoryName" />
      <el-table-column label="是否发布" align="center" prop="isPublish">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.blog_publish_status" :value="scope.row.isPublish"/>
        </template>
      </el-table-column>
      <el-table-column label="开启评论" align="center" prop="openComment">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.blog_comment_status" :value="scope.row.openComment"/>
        </template>
      </el-table-column>
      <el-table-column label="点击数" align="center" prop="clickCount" />
      <el-table-column label="排序字段" align="center" prop="sortNum" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['blog:blog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['blog:blog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改博客对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="博客标题" prop="title">
                <el-input v-model="form.title" placeholder="请输入博客标题" />
            </el-form-item>
            <el-form-item label="博客简介" prop="summary">
                <el-input v-model="form.summary" placeholder="" />
            </el-form-item>

            <el-row>

              <el-col :span="12">
                <el-form-item label="博客分类" prop="categoryId">
                  <el-select v-model="form.categoryId" placeholder="请选择分类">
                    <el-option
                      v-for="item in categoryOptions"
                      :key="item.categoryId"
                      :label="item.categoryName"
                      :value="item.categoryId"
                      :disabled="item.status == 0"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="博客标签">
                  <el-select v-model="form.tagIds" multiple :multiple-limit="tagLimit" size="medium" placeholder="请选择">
                    <el-option
                      v-for="item in tagOptions"
                      :key="item.tagId"
                      :label="item.tagName"
                      :value="item.tagId"
                      :disabled="item.status == 0"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>

              <el-col :span="8">
                <el-form-item label="发布状态" prop="isPublish">
                  <el-radio-group v-model="form.isPublish">
                    <el-radio
                    v-for="dict in dict.type.blog_publish_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                    border
                    >{{dict.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="评论状态" prop="openComment">
                  <el-radio-group v-model="form.openComment">
                    <el-radio
                    v-for="dict in dict.type.blog_comment_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                    border
                    >{{dict.label}}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="显示顺序" prop="sortNum">
                  <el-input-number v-model="form.sortNum" controls-position="right" :min="0" />
                </el-form-item>
              </el-col>

            </el-row>

          </el-col>
          <el-col :span="6">
            <el-form-item label="标题图片">
                <imageUpload v-model="form.pic" :limit="imageLimit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="博客内容">
          <editor v-model="form.content" :min-height="192"/>
          <!-- <CKEditor 
            :content="form.content" 
            :disabled="false"
            :min-height="400"
            @contentChange="updateContent">
          </CKEditor> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBlog, getBlog, delBlog, addBlog, updateBlog } from "@/api/blog/blog";
import { getSelectBlogCategoryList } from "@/api/blog/category";
import { getSelectBlogTagList } from "@/api/blog/tag";

export default {
  name: "Blog",
  dicts: ['blog_publish_status', 'blog_comment_status'],
  components: {
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分类列表
      categoryOptions: [],
      // 标签列表
      tagOptions: [],
      // 博客表格数据
      blogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 封面数量限制
      imageLimit: 1,
      // 标签数量限制
      tagLimit: 3,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        categoryId: undefined,
        tagId: undefined,
        isPublish: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "博客标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "博客内容不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "请选择博客分类", trigger: "blur" }
        ],
        isPublish: [
          { required: true, message: "请选择是否发布", trigger: "blur" }
        ],
        sortNum: [
          { required: true, message: "排序字段不能为空", trigger: "blur" }
        ],
        openComment: [
          { required: true, message: "评论状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getSelectBlogCategory();
    this.getSelectBlogTag();
  },
  methods: {
    // 富文本编辑器内容修改触发
    updateContent(content) {
      console.log(content)
      this.form.content = content
    },
    /** 查询博客列表 */
    getList() {
      this.loading = true;
      listBlog(this.queryParams).then(response => {
        this.blogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询所有博客分类 */
    getSelectBlogCategory() {
      getSelectBlogCategoryList().then(response => {
        this.categoryOptions = response.data;
      });
    },
    /** 查询所有标签 */
    getSelectBlogTag() {
      getSelectBlogTagList().then(response => {
        this.tagOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        blogId: undefined,
        pic: undefined,
        title: undefined,
        summary: undefined,
        content: undefined,
        categoryId: undefined,
        tagIds: [],
        isPublish: 1,
        sortNum: 0,
        openComment: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.blogId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加博客";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const blogId = row.blogId || this.ids
      getBlog(blogId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改博客";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.blogId != null) {
            updateBlog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBlog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const blogIds = row.blogId || this.ids;
      this.$modal.confirm('是否确认删除博客编号为"' + blogIds + '"的数据项？').then(() => {
        this.loading = true;
        return delBlog(blogIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
        this.$download.excel('/blog/blog/export', this.queryParams);
    }
  }
};
</script>